package com.leetcode.algorithm.y22.m09.w3;

import com.leetcode.algorithm.common.TreeNode;

/**
 * 129. 求根节点到叶节点数字之和
 * 
 * https://leetcode.cn/problems/sum-root-to-leaf-numbers/
 * 
 * @author jie.deng
 *
 */
class Question0129Solution01 {

	public int sumNumbers(TreeNode root) {
		return dfs(root, 0);
	}

	private int dfs(TreeNode root, int num) {
		num *= 10;
		num += root.val;
		if (root.left == null && root.right == null) {
			return num;
		}

		int sum = 0;
		if (root.left != null) {
			sum += dfs(root.left, num);
		}
		if (root.right != null) {
			sum += dfs(root.right, num);
		}
		return sum;
	}

}